package com.dwjm.exam.stu_list;

import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/deleteStudent")
public class DelStudentHandler extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("UTF-8");
//        resp.setCharacterEncoding("UTF-8");
//        resp.setContentType("application/json;charset=UTF-8");
//
//        String studentId = req.getParameter("studentId");
//        StudentDaoImpl studentDao = new StudentDaoImpl();
//        boolean i = studentDao.deleteStudent(studentId);
//
//        if (i) {
//            // Return JSON response instead of plain text
//            String jsonResponse = "{\"success\":true,\"message\":\"删除成功\"}";
//            resp.getWriter().write(jsonResponse);
//        } else {
//            String jsonResponse = "{\"success\":false,\"message\":\"删除失败\"}";
//            resp.getWriter().write(jsonResponse);
//        }
        // 设置请求和响应的字符编码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");

        // 准备响应数据
        Map<String, Object> resultMap = new HashMap<>();

        // 检查用户权限，只允许管理员删除学生
        HttpSession session = req.getSession();
        String userType = (String) session.getAttribute("userType");

        if (!"admin".equals(userType)) {
            resultMap.put("code", 403);
            resultMap.put("msg", "无权限执行此操作，只有管理员可以删除学生信息");
            new Gson().toJson(resultMap, resp.getWriter());
            return;
        }

        // 获取学生ID参数
        String studentId = req.getParameter("studentId");

        // 验证参数
        if (studentId == null || studentId.trim().isEmpty()) {
            resultMap.put("code", 400);
            resultMap.put("msg", "学生ID不能为空");
            new Gson().toJson(resultMap, resp.getWriter());
            return;
        }

        try {
            // 创建DAO实例
            StudentDao studentDao = new StudentDaoImpl();

            // 执行删除操作
            boolean success = studentDao.deleteStudent(studentId);

            if (success) {
                resultMap.put("code", 200);
                resultMap.put("msg", "删除成功");
            } else {
                resultMap.put("code", 404);
                resultMap.put("msg", "未找到对应学生记录");
            }

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", 500);
            resultMap.put("msg", "删除失败：" + e.getMessage());
        }

        // 将结果转换为JSON并输出
        new Gson().toJson(resultMap, resp.getWriter());
    }


}
